package com.pj.service.Impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pj.conf.RagflowConfig;
import com.pj.service.RagFlowChatAssistantService;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzy
 * @date 2025/6/21 13:51
 * @description : 聊天助手
 */
@Service
public class RagFlowChatAssistantServiceImpl implements RagFlowChatAssistantService {

    private final WebClient webClient;
    private final RagflowConfig ragflowConfig;

    public RagFlowChatAssistantServiceImpl(WebClient.Builder webClient, RagflowConfig ragflowConfig) {
        this.webClient = webClient.baseUrl(ragflowConfig.getBaseUrl()).build();
        this.ragflowConfig = ragflowConfig;
    }

    @Override
    public Map<String, Object> addChatAssistant(String RagFlowApi, Map<String, Object> requestBody) {
        // 请求并直接转换成 Map
        Map<String, Object> response = webClient.post()
                .uri(RagFlowApi)
                .header("Authorization", "Bearer " + ragflowConfig.getApiKey())
                .bodyValue(requestBody)
                .retrieve()
                .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
                .block();

        Map<String, Object> result = new HashMap<>();

        if (response != null && Integer.valueOf(response.get("code").toString()) == 0) {
            // 成功，直接返回 data 对象
            Object data = response.get("data");
            if (data != null) {
                result = new ObjectMapper().convertValue(
                        data, new TypeReference<Map<String, Object>>() {}
                );
                System.out.println("创建成功：" + result.get("name"));
            }
        } else if (response != null) {
            // 失败，返回错误信息
            result.put("code", response.get("code"));
            result.put("message", response.get("message"));
            System.out.println("创建失败，错误信息：" + response.get("message"));
        } else {
            // 无响应情况
            result.put("code", -1);
            result.put("message", "接口响应为空！");
            System.out.println("接口响应为空！");
        }

        return result;
    }




    /**
     * 列出聊天助手
     * @param RagFlowApi
     * @param requestBody
     * @return
     */
    @Override
    public List<Map<String, Object>> listChatAssistant(String RagFlowApi, Map<String, Object> requestBody) {
        Map<String, Object> response = webClient.get()
                .uri(RagFlowApi)
                .header("Authorization", "Bearer " + ragflowConfig.getApiKey())
                .retrieve()
                .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
                .block();

        List<Map<String, Object>> dataList = null;
        if (response != null && (int) response.get("code") == 0) {
            // 获取 data，强制转成 List<Map>
            dataList = (List<Map<String, Object>>) response.get("data");
            if (dataList != null && !dataList.isEmpty()) {
                for (Map<String, Object> item : dataList) {
                    System.out.println("聊天助手-list，名称：" + item.get("name"));
                    // 其他字段 item.get("avatar")  item.get("llm") 都可以随时拿
                }
            } else {
                System.out.println("创建成功，但列表为空！");
            }
        } else if (response != null) {
            System.out.println("创建失败，错误信息：" + response.get("message"));
        } else {
            System.out.println("接口响应为空！");
        }

        return dataList;
    }

}
